<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/11
 * Time: 15:41
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\shop\model\ConfigsModel;

class Configs extends Admin
{
    public function index($type='weixin')
    {
        $list_tab = [
//            'siteConfig' => ['title' => '网站基本配置', 'url' => url('index',['type'=>'siteConfig'])],
            'weixin' => ['title' => '公众号配置', 'url' => url('index',['type'=>'weixin'])],
            'program' => ['title' => '小程序配置', 'url' => url('index',['type'=>'program'])],
            'weixinpay' => ['title' => '微支付配置', 'url' => url('index',['type'=>'weixinpay'])],
            'distribution' => ['title' => '分销配置', 'url' => url('index',['type'=>'distribution'])],
        ];
        switch ($type)
        {
            case 'weixin':
                return $this->weixin($list_tab,$type);
                break;
            case 'program':
                return $this->program($list_tab,$type);
                break;
            case 'weixinpay':
                return $this->weixinpay($list_tab,$type);
                break;
            case 'distribution':
                return $this->distribution($list_tab,$type);
                break;
            default:
                return $this->weixin($list_tab,$type);
        }
    }


    // 微信公众号配置
    private function weixin($list_tab, $type)
    {
        $data = ConfigsModel::getConfigItem('weixin');
        return ZBuilder::make('form')
            ->setPageTitle('微信公众号配置')
            ->setTabNav($list_tab, $type)
            ->setUrl(url('store'))
            ->addHidden('type','weixin')
            ->addStatic('url','URL(服务器地址)')
            ->addText('token','token','用户微信token配置')
            ->addText('appid','appid')
            ->addText('appsecret','appsecret ')
            ->addText('name','公众号名称')
            ->addText('weixin_code','微信号')
            ->addImage('qr_code','公众号二维码')
            ->setFormData($data)
            ->fetch();
    }

    // 微信小程序配置
    private function program($list_tab, $type)
    {
        $data = ConfigsModel::getConfigItem('program');
        return ZBuilder::make('form')
            ->setPageTitle('微信小程序配置')
            ->setTabNav($list_tab,$type)
            ->setUrl(url('store'))
            ->addHidden('type','program')
            ->addText('appid','appid')
            ->addText('appsecret','appsecret')
            ->setFormData($data)
            ->fetch();
    }

    // 微信支付配置
    private function weixinpay($list_tab, $type)
    {
        $data = ConfigsModel::getConfigItem('weixinpay');
        return ZBuilder::make('form')
            ->setPageTitle('微信支付配置')
            ->setTabNav($list_tab,$type)
            ->setUrl(url('store'))
            ->addHidden('type','weixinpay')
            ->addText('appid','appid')
            ->addText('appsecret','appsecret')
            ->addText('mch_id','商户号')
            ->addText('mch_secret','商户密钥')
            ->setFormData($data)
            ->fetch();
    }

    // 分销配置
    private function distribution($list_tab, $type)
    {
        $data = ConfigsModel::getConfigItem('distribution');
        $day_list = [];
        for ($i = 1; $i <= 30; $i++){
            $day_list[$i] = $i.'天';
        }
        return ZBuilder::make('form')
            ->setPageTitle('分销配置')
            ->setTabNav($list_tab,$type)
            ->setUrl(url('store'))
            ->addHidden('type','distribution')
            ->addSwitch('switch','分销开关')
            ->addRadio('condition','成为分销商条件','',['1'=>'无条件成为','2'=>'购买商品后成为','3'=>'消费满一定额度'])
            ->addText('sell_name','分销商名称')
            ->addText('first_leader_name','一级分销商名称')
            ->addText('first_leader_val','一级分销商比例','单位%')
            ->addText('second_leader_name','二级级分销商名称')
            ->addText('second_leader_val','二级级分销商比例','单位%')
            ->addSelect('get_time','分佣时间','订单确认完成后多少天可以收到分佣',$day_list)
            ->setFormData($data)
            ->fetch();
    }

    public function store()
    {
        $data = input('post.');
        $name = $data['type'];
        unset($data['__token__']);
        unset($data['type']);
        $res = ConfigsModel::saveConfigItem($name,$data);
        if($res){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }

}